package com.atguigu.juc.one;


import javax.lang.model.SourceVersion;
import java.sql.SQLOutput;

//资源类
class Ticket {
    //确定票数
    private Integer num = 30;
    //判断是否有票
    public  synchronized void sale(){   //单线程操作
        if(num <=0 ){
            System.out.println("票已售完");
            return;
        }
        //业务流程
        System.out.println(Thread.currentThread().getName()+"开始卖票");
        //有锁,睡觉效果更佳啊！！
      //  Thread.sleep();
        num--;
        System.out.println(Thread.currentThread().getName()+"剩余票数："+num);
    }
}
public class TicketDemo {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        //写多线程位置
        for (int i = 0; i <100 ; i++) {
            new Thread(()->{
                ticket.sale();
            },String.valueOf(i+1)).start();  //String.valueOf(i+1)是线程名称
        }
    }
}
